package com.example.bookserver.controller;

import com.example.BOOKServer.bean.Books;
import com.example.BOOKServer.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/BOOKApp")
public class BookController {
    @Autowired
    BookDao bookDao;
    Map<String,Object> map=new HashMap<>();
    //list
    @ResponseBody
    @RequestMapping("/book/list")
    public Map<String,Object>bookList(){
        List<Books>list=bookDao.findAll();
        if (list!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",list);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    //id_Inquire
    @ResponseBody
    @RequestMapping("/book/{id}")
    public Map<String,Object>findBookById(@PathVariable Integer id){
        Optional<Books> optional=bookDao.findById(id);
        if (!optional.isEmpty()){
            Books books=optional.get();
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",books);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}